package com.sgb.merchant.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgb.link.entity.merchant.PublishIndustryEntity;
import com.sgb.link.router.PathMerchant;
import com.sgb.link.view.widget.MyItemDecoration;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantActivitySelectFacilityBinding;
import com.sgb.merchant.databinding.MerchantDialogAddTagBinding;
import com.sgb.merchant.view.adapter.FacilityTagAdapter;
import com.sgb.merchant.viewmodel.IndustryViewModel;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.recyclerview.FlowLayoutManager;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author: 王彬臣
 * @date: 2021/10/14
 * @Description: 选择惠及设施页面
 */
@Route(path = PathMerchant.SelectFacilityCityActivity)
public class SelectFacilityCityActivity extends BaseActivity<IndustryViewModel, MerchantActivitySelectFacilityBinding> {

    FacilityTagAdapter mAdapter;

    @Autowired(name = RouterKey.DATA)
    ArrayList<PublishIndustryEntity.ParkLabelsBean> mTagList;

    @Override
    protected int layoutId() {
        return R.layout.merchant_activity_select_facility;
    }

    @Override
    protected void createViewModel() {
       mViewModel=new ViewModelProvider(this).get(IndustryViewModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        if(mTagList==null){
            mTagList=new ArrayList<>();
        }
        mBinding.setOnClick(this);
        mBinding.actionBar.setOnBackClickListener(this);
        mBinding.rvTag.setLayoutManager(new FlowLayoutManager());
        mBinding.rvTag.addItemDecoration(new MyItemDecoration(this));
        mAdapter=new FacilityTagAdapter(mTagList);
        mAdapter.bindToRecyclerView(mBinding.rvTag);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId()==R.id.iv_delete){
                    mTagList.remove(position);
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back_red) {
            finish();
        } else if (id == R.id.tv_confirm) {
            if (mTagList.size() == 0) {
                MToast.showToast(SelectFacilityCityActivity.this, "请添加标签");
                return;
            }
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("tagList", (Serializable) mTagList);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        } else if (id == R.id.tv_add) {
            if (mTagList.size() == 10) {
                MToast.showToast(this, "最多可添加10个标签");
                return;
            }
            showAddTagDialog();
        }
    }
    public void showAddTagDialog(){

        View view = View.inflate(this, R.layout.merchant_dialog_add_tag, null);
        MerchantDialogAddTagBinding mTagBinding=DataBindingUtil.bind(view);
        RoundCornerDialog dialog=new RoundCornerDialog(this, 0, 0, view, R.style.RoundCornerDialog);
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
        mTagBinding.tvLogoutCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        mTagBinding.tvLogoutConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag=mTagBinding.ediInput.getText().toString();
                if(TextUtils.isEmpty(tag)){
                    MToast.showToast(SelectFacilityCityActivity.this,"请输入惠及配套");
                    return;
                }
                boolean isHave=false;
                for(PublishIndustryEntity.ParkLabelsBean tagContent:mTagList){
                    if(tagContent.getLabelName().equals(tag)){
                        isHave=true;
                        MToast.showToast(SelectFacilityCityActivity.this,"配套名称重复，请重新添加");
                        break;
                    }
                }
                if(isHave){
                    return;
                }


                PublishIndustryEntity.ParkLabelsBean data=new PublishIndustryEntity.ParkLabelsBean();
                data.setLabelSort(1);
                data.setLabelName(tag);
                mTagList.add(data);
                mAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });

    }
}
